//
//  ContainerViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "ContainerViewController.h"
#import "MyViewController.h"
#import "FindViewController.h"
#import "MusicBankViewController.h"
#import "LoveView.h"
#import "SearchAllViewController.h"
@interface ContainerViewController ()<UISearchResultsUpdating, UISearchBarDelegate, UISearchBarDelegate, HQloveView>
@property (nonatomic, retain) MyViewController *myViewController;
@property (nonatomic, retain) FindViewController *findViewController;
@property (nonatomic, retain) MusicBankViewController *musicBankViewController;
@property (nonatomic, retain) UISearchController *searchController;
@property (nonatomic, retain) LoveView *loveView;
@end

@implementation ContainerViewController

- (void)dealloc
{
    [_myViewController release];
    [_findViewController release];
    [_musicBankViewController release];
    [_searchController release];
    [_loveView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建导航栏
    [self creatNavBar];
    //创建视图
    [self creatChildView];
}
#pragma 创建容器视图控制器
- (void)creatChildView
{
    self.myViewController = [MyViewController new];
    self.musicBankViewController = [MusicBankViewController new];
    self.findViewController = [FindViewController new];
    [self addChildViewController:self.myViewController];
    [self addChildViewController:self.musicBankViewController];
    [self addChildViewController:self.findViewController];
    [self.view addSubview:self.myViewController.view];
    [self.view addSubview:self.musicBankViewController.view];
    [self.view addSubview:self.findViewController.view];
    [_musicBankViewController release];
    [_myViewController release];
    [_findViewController release];
    [self.view bringSubviewToFront:self.myViewController.view];
}
#pragma 导航栏左视图触发事件
- (void)leftBar:(UIBarButtonItem *)sender
{
    [self.delegate ChangeWindowInAppDelegate];
}
#pragma 导航栏右视图触发事件
- (void)RightBar:(UIBarButtonItem *)sender
{
    [self presentViewController:self.searchController animated:YES completion:^{
    }];
    [self.loveView getData];
    [self.searchController.view addSubview:self.loveView];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSString *local = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingString:@"/arr.txt"];
    NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:local];
    if (nil == arr)
    {
        arr = [NSMutableArray array];
    }
    if (![arr containsObject:self.searchController.searchBar.text])
    {
        [arr addObject:self.searchController.searchBar.text];
        [arr writeToFile:local atomically:YES];
    }
    [self dismissViewControllerAnimated:NO completion:nil];
    [self change:self.searchController.searchBar.text];
}
- (void)getButtonTitle:(NSString *)str
{
    NSString *local = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingString:@"/arr.txt"];
    NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:local];
    if (nil == arr)
    {
        arr = [NSMutableArray array];
    }
    if (![arr containsObject:str])
    {
        [arr addObject:str];
        [arr writeToFile:local atomically:YES];
    }
    [self dismissViewControllerAnimated:NO completion:nil];
    [self change:str];
}
- (void)change:(NSString *)str
{
    SearchAllViewController *search = [SearchAllViewController new];
    [self.navigationController pushViewController:search animated:YES];
    search.str = str;
    [search release];
    self.searchController.searchBar.text = @"";
}
- (LoveView *)loveView
{
    if (nil == _loveView)
    {
        self.loveView = [[LoveView alloc] initWithFrame:CGRectMake(0, 70, 375, 600)];
        self.loveView.delegate = self;
        [_loveView release];
    }
    return _loveView;
}
- (void)barResignFirstResponder
{
    [self.searchController.searchBar resignFirstResponder];
}
- (UISearchController *)searchController
{
    if (nil == _searchController) {
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        self.searchController.searchResultsUpdater = self;
        self.searchController.searchBar.delegate = self;
        self.searchController.searchBar.placeholder = @"歌曲,歌手,专辑";
        self.searchController.dimsBackgroundDuringPresentation = YES;
        self.searchController.hidesNavigationBarDuringPresentation = NO;
        self.searchController.obscuresBackgroundDuringPresentation = YES;
        self.searchController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"backGroud" ofType:@"jpg"]]];
        [self.searchController.searchBar setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"nav" ofType:@"jpg"]]]];
        self.searchController.searchBar.showsCancelButton = YES;
        // 修改取消按键
        for (UIView *view in self.searchController.searchBar.subviews[0].subviews)
        {
            if ([view isKindOfClass:NSClassFromString(@"UINavigationButton")])
            {
                UIButton *btn = (UIButton *)view;
                [btn setTitle:@"取消" forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0] forState:UIControlStateNormal];
            }
            if ([view isKindOfClass:[UITextField class]])
            {
                UITextField *text = (UITextField *)view;
                text.backgroundColor = [UIColor colorWithRed:36 / 255.0 green:47 / 255.0 blue:55 / 255.0 alpha:1.0];
                text.textColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
            }
        }
    }
    return _searchController;
}
#pragma 导航栏中视图触发事件
- (void)buttonAction:(UIButton *)sender
{
    UIButton *MyButton = (UIButton *)[self.navigationItem.titleView viewWithTag:1301];
    UIButton *MusicButton = (UIButton *)[self.navigationItem.titleView viewWithTag:1302];
    UIButton *FindButton = (UIButton *)[self.navigationItem.titleView viewWithTag:1303];
    if (!sender.selected)
    {
        CATransition *tran = [CATransition animation];
        tran.type = @"rippleEffect";
        tran.repeatCount = 1;
        tran.duration = 1;
        [sender.layer addAnimation:tran forKey:@"168"];
    }
    if (1301 == sender.tag)
    {
        MyButton.selected = YES;
        MusicButton.selected = NO;
        FindButton.selected = NO;
        MyButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
        MusicButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        FindButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [self.view bringSubviewToFront:self.myViewController.view];
    } else if (1302 == sender.tag) {
        MyButton.selected = NO;
        MusicButton.selected = YES;
        FindButton.selected = NO;
        MyButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        MusicButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
        FindButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [self.view bringSubviewToFront:self.musicBankViewController.view];
    } else {
        MyButton.selected = NO;
        MusicButton.selected = NO;
        FindButton.selected = YES;
        MyButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        MusicButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        FindButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
        [self.view bringSubviewToFront:self.findViewController.view];
    }
}

- (void)creatNavBar
{
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"HomePageNavBarLeft"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBar:)] autorelease];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"HomePageNavBarRight"] style:UIBarButtonItemStylePlain target:self action:@selector(RightBar:)] autorelease];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 7, 150, 50)];
    UIButton *Mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
    Mybutton.frame = CGRectMake(0, 0, 50, 50);
    [Mybutton setTitle:@"我的" forState:UIControlStateNormal];
    [Mybutton setTitleColor:[UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0] forState:UIControlStateSelected];
    Mybutton.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [Mybutton setTitleColor:[UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    [Mybutton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    Mybutton.selected = YES;
    Mybutton.tag = 1301;
    [view addSubview:Mybutton];
    
    UIButton *Musicbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    Musicbutton.frame = CGRectMake(50, 0, 50, 50);
    [Musicbutton setTitle:@"乐库" forState:UIControlStateNormal];
    [Musicbutton setTitleColor:[UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0] forState:UIControlStateSelected];
    [Musicbutton setTitleColor:[UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    Musicbutton.titleLabel.font = [UIFont systemFontOfSize:16];
    [Musicbutton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    Musicbutton.selected = NO;
    Musicbutton.tag = 1302;
    [view addSubview:Musicbutton];
    
    UIButton *FindButton = [UIButton buttonWithType:UIButtonTypeCustom];
    FindButton.frame = CGRectMake(100, 0, 50, 50);
    [FindButton setTitle:@"发现" forState:UIControlStateNormal];
    [FindButton setTitleColor:[UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0] forState:UIControlStateSelected];
    [FindButton setTitleColor:[UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    FindButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [FindButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    FindButton.selected = NO;
    FindButton.tag = 1303;
    [view addSubview:FindButton];
    self.navigationItem.titleView = view;
    [view release];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeControlView" object:self userInfo:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
